package flow.service;

import com.alibaba.fastjson.JSONObject;
import flow.action.FlowItemTestAction;
import flow.model.*;
import m.appserver.ActionRedirect;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.util.ArrayUtil;
import m.system.util.GenerateID;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FlowItemTestService extends FlowService{
    @Override
    public String getFlowIdentity(String busiParam) {
        return "flowTest";
    }

    @Override
    public ActionRedirect getRedirectPage(FlowBusiness business, AdminLogin admin, OrgGroupView org, boolean edit) throws Exception {
        FlowItemTest model=ModelQueryList.getModel(FlowItemTest.class, business.getOid(),"*","dutyOrg.*","dutyUser.*","applyUser.*","applyOrg.*");
        List<FlowItemPhase> list;
        if(null==model){
            model=new FlowItemTest();
            model.setApplyUser(admin);
            model.setApplyOrg(org);
            model.setCreateDate(new Date());
            model.setFlowStatus("C");
            model.setFlowRemark(admin.getRealname()+"申请");
            list=new ArrayList<>();
        }else{
            list=ModelQueryList.getModelList(FlowItemPhase.class,new String[]{"*"},null,
                QueryCondition.eq("item.oid",model.getOid()), QueryOrder.asc("oid"));
        }
//        return new ActionRedirect(
//            new VuePageResult("page/base/flow/flowItemTest.js")
//                .addParam("model", model)
//                .addParam("edit",edit)
//        );
        JSONMessage param=new JSONMessage().push("model",model).push("list",list).push("edit",edit);
        return new ActionRedirect(FlowItemTestAction.class,"toEdit",param);
    }

    private String nextUser(List<FlowInstanceSection> nextInsSec){
        List<String> us=new ArrayList<>();
        for(FlowInstanceSection is : nextInsSec){
            us.add(is.getUser().getRealname());
        }
        return ArrayUtil.connection(us.toArray(new String[0]),",");
    }
    private FlowItemTest saveModel(FlowInstance ins, List<FlowInstanceSection> nextInsSec, JSONObject flowModel) throws Exception {
        FlowItemTest model=flowModel.getJSONObject("model").toJavaObject(FlowItemTest.class);
        if(ObjectUtil.isEmptyOid(model)){
            model.setOid(ins.getBusiness().getOid());//使用business.oid当业务主键 必须
            model.setFlowStatus("C");
            model.setFlowRemark("草稿("+nextUser(nextInsSec)+")");
            ModelUpdateUtil.insertModel(model);
        }else{
            ModelUpdateUtil.updateModel(model);
        }
        //更新业务描述 必须
        this.updateBusinessDesc(model.getOid(),"流程项目测试",model.getItemName());
        List<JSONObject> list=flowModel.getJSONArray("list").toJavaList(JSONObject.class);
        for(JSONObject json : list){
            if(json.getIntValue("deleteFlag")==1){//tableForm存在oid删除时添加的deleteFlag标识，1代表删除
                ModelUpdateUtil.deleteModel(json.toJavaObject(FlowItemPhase.class));
            }else{
                FlowItemPhase phase=json.toJavaObject(FlowItemPhase.class);
                phase.setItem(model);
                if(ObjectUtil.isEmptyOid(phase)){
                    phase.setOid(GenerateID.generatePrimaryKey());
                    ModelUpdateUtil.insertModel(phase);
                }else{
                    ModelUpdateUtil.updateModel(phase);
                }
            }
        }
        return model;
    }
    @Override
    public void saveFlowAfter(FlowInstance ins, List<FlowInstanceSection> nextInsSec, JSONObject flowModel) throws Exception {
        saveModel(ins,nextInsSec,flowModel);
    }
    @Override
    public void processFlowAfter(String param, FlowInstance ins, List<FlowInstanceSection> nextInsSec, JSONObject flowModel) throws Exception {
        FlowItemTest model=saveModel(ins,nextInsSec,flowModel);
        if(nextInsSec.isEmpty()){//空代表没有下一步执行人，也可以根据param和ins判断流程是否完成
            model.setFlowStatus("Y");//审核完成
            model.setFlowRemark("审核完成");
        }else {
            model.setFlowStatus("A");//审核中
            model.setFlowRemark(ins.getCurrentSection().getName() + "(" + nextUser(nextInsSec) + ")");
        }
        ModelUpdateUtil.updateModel(model,"flowStatus","flowRemark");
    }

    @Override
    public void forwardFlowAfter(FlowInstance ins, List<FlowInstanceSection> nextInsSec) throws Exception {
        FlowItemTest model=new FlowItemTest();
        model.setOid(ins.getBusiness().getOid());
        model.setFlowRemark(ins.getCurrentSection().getName()+"("+nextUser(nextInsSec)+")");
        ModelUpdateUtil.updateModel(model,"flowRemark");
    }
}
